<template>
  <div ref="chart" style="height:300px;width:100%" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: ['checkCompanyRankList'],
  data() {
    return {
      chart: null,
      seriesData: [],
      loading: false,
    }
  },
  watch: {
    checkCompanyRankList: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.loading = true
      if (!this.$refs.chart) {
        return;
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(
        {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {},
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: this.checkCompanyRankList.map(o => o.companyName),
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '检测数量',
              type: 'bar',
              data: this.checkCompanyRankList.map(o => o.count)
            },
            {
              name: '不合格数量',
              type: 'bar',
              data: this.checkCompanyRankList.map(o => o.outCount)
            }
          ]
        }
      )
      this.loading = false
    }
  }
}
</script>

<style scoped>

</style>
